$(function() {

	$("#grid").jqGrid({
		url : '../sys/button/list',
		datatype : "json",
		colModel : [ {
			label : '按钮编号',
			name : 'buttonCode',
			width : 40,
			key : true,
			sortable : true
		}, {
			label : '按钮名称',
			name : 'buttonName',
			width : 60
		}, {
			label : '授权标识',
			name : 'accessCode',
			width : 100
		}, {
			label : '排序字段',
			name : 'orderColumn',
			width : 100
		} ],
		viewrecords : true,
		height : GetWindowHeight(140),
		rowNum : 10,
		rowList : [ 10, 30, 50 ],
		rownumbers : true,
		rownumWidth : 25,
		autowidth : true,
		multiselect : true,
		pager : "#gridPager",
		jsonReader : {
			root : "page.list",
			page : "page.currPage",
			total : "page.totalPage",
			records : "page.totalCount"
		},
		prmNames : {
			page : "page",
			rows : "limit",
			order : "order"
		},
		gridComplete : function() {
			// 隐藏grid底部滚动条
			$("#grid").closest(".ui-jqgrid-bdiv").css({
				"overflow-x" : "hidden"
			});
		}
	});
});

var vm = new Vue({
	el : '#app',
	data : {
		showList : true,
		title : null,
		button : {
			orderColumn : 0
		},
		rules : {
			buttonName : [ {
				required : true,
				message : '请输入按钮名称',
				trigger : 'blur'
			}, {
				min : 2,
				max : 5,
				message : '长度在 2 到 5 个字符',
				trigger : 'blur'
			} ],
			accessCode: [
	            { required: true, message: '请输入授权标识', trigger: 'blur' }
	        ]
		}
	},
	methods : {
		add : function() {
			vm.showList = false;
			vm.title = "新增按钮";
			vm.button = {
				buttonName : null,
				accessCode : null,
				orderColumn : 0
			};
		},
		update : function(event) {
			var code = getSelectedRow(this);
			if (code == null) {
				return;
			}
			$.get("../sys/button/info/" + code, function(r) {
				vm.showList = false;
				vm.title = "修改按钮";
				vm.button = r.button;
			});
		},
		del : function(event) {
			var btn = this;
			var ids = getSelectedRows(btn);
			if (ids == null) {
				return;
			}
			confirm('确定要删除选中的记录？', function() {
				$.ajax({
					type : "POST",
					url : "../sys/button/delete",
					data : JSON.stringify(ids),
					success : function(r) {
						if (r.code === 0) {
							showSuccess(btn, "删除成功！");
							vm.reload();
						} else {
							showError(btn, r.msg);
						}
					}
				});
			});
		},
		saveOrUpdate : function(form) {
			var btn = this;
			this.$refs[form].validate(function(valid) {
				if (valid) {
					var url = vm.button.buttonCode == null ? "../sys/button/add"
							: "../sys/button/update";
					$.ajax({
						type : "POST",
						url : url,
						data : JSON.stringify(vm.button),
						success : function(r) {
							if (r.code === 0) {
								showSuccess(btn, "保存成功！");
								vm.reload();
							} else {
								showError(btn, r.msg);
							}
						}
					});
				}
	        });
		},
		reload : function(event) {
			vm.showList = true;
			var page = $("#grid").jqGrid('getGridParam', 'page');
			$("#grid").jqGrid('setGridParam', {
				page : page
			}).trigger("reloadGrid");
		},
		resetForm : function(form) {
	        this.$refs[form].resetFields();
		}
	}
});